<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>Document</title>
  <style>
    .input-wrap {
      width: 240px;
      height: 32px;
      border: 1px solid;
      border-radius: 2px;
    }
    input {
      border: 0;
      width: 200px;
      height: 30px;
      outline: none;
    }
  </style>
</head>
<body>
  <div id="app">
    <h2>封装input组件</h2>

    <!-- 当组件的自定义属性，在子组件没有被props接受，那么这个属性就会挂载到子组件的根元素上 -->
    <el-input width="300" maxlength="9" :value="value" @aaa="fn"></el-input>
  </div>

  <script src="../vue3.js"></script>
  <script>
    const {createApp} = Vue;


    // 这里就是要封装的需要限制长度的input组件
    const ElInput = {
      props: ["width", "maxlength", "value"],
      data() {
        return {
          color: "#aaa",
        }
      },
      methods: {
        inputFocus() {
          this.color = "blue";
        },
        inputBlur() {
          this.color = "#aaa";
        },
        inputInput(e) {
          // const v = e.target.value.slice(0, +this.maxlength);
          // console.log(v);
          
          this.$emit("aaa", e.target.value);
        }
      },
      template: `
        <div class="input-wrap" :style="{ borderColor: color, width: width + 'px' }">
          <input type="text" @focus="inputFocus" @blur="inputBlur" :style="{width: width - 60 + 'px'}" :value="value" @input="inputInput" />
          <span>{{ value.length }} / {{ maxlength }}</span>
        </div>
      `
    }


    const app = createApp({
      components: {
        ElInput
      },
      data() {
        return {
          value: "123"
        }
      },
      methods: {
        fn(v) {
          this.value = v;
        }
      }
    });

    app.mount("#app");
  </script>
</body>
</html>